// https://leetcode.cn/problems/as-far-from-land-as-possible/description/
#include <bits/stdc++.h>
using namespace std;

/**
 * 多元bfs
 */
int bfs(vector<vector<int>> &g) {
    struct Node {
        int x, y, v;
    };
    deque<Node> que;
    vector<vector<bool>> flag(g.size(), vector<bool>(g[0].size()));
    vector<vector<int>> ans(g.size(), vector<int>(g[0].size(), -1)); 
    //加入起点
    for (int i = 0; i < g.size(); i++) {
        for (int j = 0; j < g[0].size(); j++) {
            if (g[i][j] == 1) {
                que.push_back({i, j, 0});
            }
        }
    }
    struct Dir {
        int x, y;
    }d[4] = {1, 0, -1, 0, 0, 1, 0, -1};
    //更秀的写法
    int dir[5] = {-1, 0, 1, 0, -1};
    //每次x移动dir[i], y移动dir[i + 1];
    while (!que.empty()) {
        Node now = que.front(); que.pop_front();
        for (int i = 0; i < 4; i++) {
            int nx = now.x + d[i].x, ny = now.y + d[i].y;
            if (nx >= 0 && nx < g.size() && ny >= 0 && ny < g.size()) {
                if (flag[nx][ny]) continue;
                if (g[nx][ny] == 0) {
                    ans[nx][ny] = now.v + 1;
                    que.push_back({nx, ny, now.v + 1});
                    flag[nx][ny] = true;
                }
            }
        }
    }
    int res = -1;
    for (int i = 0; i < g.size(); i++) {
        for (int j = 0; j < g.size(); j++) {
           res = max(res, ans[i][j]); 
        }
    }
    return res;
}

int maxDistance(vector<vector<int>>& grid) {
    return bfs(grid);
}

int main() {
    
    return 0;
}